package com.cskaoyan._04sort;

import java.util.Arrays;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/16 11:16
 **/
/*
定义一个学生类，让其按照学生的年龄的大小，从小到大进行排序
 */
public class ComparableTest {
    public static void main(String[] args) {
        // 创建学生对象数组
        Student s1 = new Student("张三", 20, 69);
        Student s2 = new Student("李四", 23, 59);
        Student s3 = new Student("王五", 20, 88);
        Student s4 = new Student("赵六", 25, 77);
        Student s5 = new Student("飞哥", 22, 100);
        // 填充数组
        Student[] students = {s1, s2, s3, s4, s5};
        // 排序前
        System.out.println("排序前:");
        System.out.println(Arrays.toString(students));
        // 排序后
        Arrays.sort(students);

        // 输出
        System.out.println("排序后:");
        System.out.println(Arrays.toString(students));
    }
}
/*
1.实现Comparable接口
2.重写compareTo方法
 */
class Student implements Comparable<Student>{
    String name;
    int age;
    int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        // 按照学生的年龄的大小，从小到大进行排序
        // return this.getAge() - o.getAge();
        // 按照学生的年龄的大小，从大到小进行排序
        //return o.getAge() - this.getAge();

        // 做一个综合排序,正常按照年龄的大小从小到大进行排序
        // 如果年龄相同,按照分数的从高到低进行排序
        if (this.getAge() == o.getAge()) {
            return o.getScore() - this.getScore();
        }

        return this.getAge() - o.getAge();

    }

    //@Override
    //public int compareTo(Object o) {
    //    return 0;
    //}
}